﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CE.User;
using CE.Global;
using CE.Provider;

public partial class Provider_CarInsurance_ProviderCarDriverDOB : System.Web.UI.Page
{
    string PageName = "CXCI";
    string SubPageName = "CXCICDRAG";
    public static Int64 _providercardriverdobid = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null || Session[CE.Global.General.ProviderCompanyId] == null)
        {
            Response.Redirect("~/Provider/ProviderSecureLogin.aspx");
        }
        CheckMainPageAccess();
        CheckSubPageAccess();

        if (!IsPostBack)
        {
            ClearProcess();
            if (Request.Params["PCDDOBId"] != null)
            {
                usrEffectivityDate.StartYear = CE.Global.General.GetUTCDateTime().Year;
                usrEffectivityDate.EndYear = CE.Global.General.GetUTCDateTime().Year + 2;
                Int64 _cardriverdobid;
                if (Int64.TryParse(Request.Params["PCDDOBId"].ToString(), out _cardriverdobid))
                {
                    ShowCarDriverDOB(_cardriverdobid);
                }
            }
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            string _authcode = CE.Security.MySecurity.Encrypt(txtAuthCode.Text, true);
            if (CheckAuthCode(_authcode, 100))
            {

                CarDriverDob _cardriverdob = new CarDriverDob();
                CarDriverDobDB _cardriverdobdb = new CarDriverDobDB();
            
                int _trantype = 1;
                _cardriverdob.ProviderCarDriverDOBId = Convert.ToInt64(lblProviderCarDriverDOBId.Text);
                if (_cardriverdob.ProviderCarDriverDOBId > 0)
                {
                    _trantype = 2;
                }

                _cardriverdob.ProviderCompanyId = Convert.ToInt32(Session[CE.Global.General.ProviderCompanyId].ToString());
                _cardriverdob.FromDOB = Convert.ToInt16(txtFromDOB.Text.Trim());
                _cardriverdob.ToDOB = Convert.ToInt16(txtToDOB.Text.Trim());
                _cardriverdob.DOBRate = Convert.ToSingle(txtDOBRate.Text.Trim());
                _cardriverdob.RefCEUserId = Convert.ToInt64(Session[CE.Global.General.UserId].ToString());
                if (chkStatus.Checked == true)
                {
                    _cardriverdob.Status = true;
                }
                else if (chkStatus.Checked == false)
                {
                    _cardriverdob.Status = false;
                }

                if (rdoEffectivity.SelectedValue == "Yes" && usrEffectivityDate.GetDate() > CE.Global.General.GetUTCDateTime())
                {
                    _cardriverdob.FInfo1 = float.Parse(txtNewRate.Text);
                    _cardriverdob.DInfo1 = usrEffectivityDate.GetDate();

                }
                else
                {
                    _cardriverdob.FInfo1 = float.Parse(txtDOBRate.Text);
                    _cardriverdob.DInfo1 = CE.Global.General.GetUTCDateTime();

                }

                _cardriverdob.ProviderCarDriverDOBId = _cardriverdobdb.Insert(_cardriverdob);

                if (_cardriverdob.ProviderCarDriverDOBId > 0)
                {
                    ClearProcess();
                    InsertProviderAuthTransaction(_cardriverdob.ProviderCarDriverDOBId, "CICarDriverAge", _trantype, 100, _authcode, _cardriverdob.ProviderCompanyId, _cardriverdob.RefCEUserId);
                    ProviderUpdateSave(_cardriverdob.ProviderCarDriverDOBId);
                    lblMessage.Text = "Updated successfully!";
                }
                else
                {
                    lblError.Text = "Updating failed!";
                }
            }
            else
            {
                lblMessage.Text = "Wrong auth code!";
            }
        }
    }

    private void ProviderUpdateSave(Int64 ProviderRefId)
    {
        ProviderUpdates _providerupdates = new ProviderUpdates();
        ProviderUpdatesDB _providerupdatesDB = new ProviderUpdatesDB();

        _providerupdates.UpdateId = 0;
        _providerupdates.CXTableId = GetCXTableId();
        _providerupdates.ProviderRefId = ProviderRefId;

        if (rdoEffectivity.SelectedValue == "Yes" && usrEffectivityDate.GetDate() > CE.Global.General.GetUTCDateTime())
        {
            //Scheduled
            _providerupdates.UpdateType = 1;
        }
        else
        {
            //Immediate
            _providerupdates.UpdateType = 0;
        }

        _providerupdates.ProviderCompanyId = Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId]);
        _providerupdates.RefCEUserID = Convert.ToInt64(Session[General.UserId].ToString());

        _providerupdatesDB.Insert(_providerupdates);

    }

    private Int32 GetCXTableId()
    {
        Int32 CXID = 0;

        ProviderTableListings _tablelist = new ProviderTableListings();
        ProviderTableListingsDB _tablelistdb = new ProviderTableListingsDB();

        int ProviderType = 100; //Car Insurance
        string CXTable = "tblProviderCarDriverDOB";

        _tablelist = _tablelistdb.GetProviderTableListingsSingleItem(ProviderType, Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId]), CXTable);

        if (_tablelist != null)
            CXID = _tablelist.CXTableId;

        return CXID;

    }

    protected void btnClearForm_Click(object sender, EventArgs e)
    {
        ClearProcess();
    }

    private void ShowCarDriverDOB(Int64 ProviderCarDriverDOBId)
    {
        CarDriverDob _cardriverdob = new CarDriverDob();
        CarDriverDobDB _cardriverdobdb = new CarDriverDobDB();
        _cardriverdob = _cardriverdobdb.GetCarDriverDob(ProviderCarDriverDOBId);
        if (_cardriverdob != null)
        {
            _providercardriverdobid = Convert.ToInt64(_cardriverdob.ProviderCarDriverDOBId.ToString());
            lblProviderCarDriverDOBId.Text = _providercardriverdobid.ToString();
            txtFromDOB.Text = _cardriverdob.FromDOB.ToString();
            txtToDOB.Text = _cardriverdob.ToDOB.ToString();
            txtDOBRate.Text = _cardriverdob.DOBRate.ToString();
            if (_cardriverdob.Status == true)
            {
                chkStatus.Checked = true;
            }
            else
            {
                chkStatus.Checked = false;
            }

            if (_cardriverdob.DInfo1 > CE.Global.General.GetUTCDateTime())
            {
                divNewRate.Visible = true;
                txtNewRate.Text = _cardriverdob.FInfo1.ToString();
                rdoEffectivity.SelectedValue = "Yes";
            }
            else
            {
                divNewRate.Visible = false;
                rdoEffectivity.SelectedValue = "No";
            }

            usrEffectivityDate.Day = _cardriverdob.DInfo1.Day;
            usrEffectivityDate.Month = _cardriverdob.DInfo1.Month;
            usrEffectivityDate.Year = _cardriverdob.DInfo1.Year;

            //btnSubmit.Text = "Update";
            //btnClearForm.Text = "Add New";
        }
    }

    public void ClearProcess()
    {
        _providercardriverdobid = 0;
        lblProviderCarDriverDOBId.Text = "0";
        txtFromDOB.Text = "";
        txtToDOB.Text = "";
        txtDOBRate.Text = "";
        chkStatus.Checked = true;
        lblMessage.Text = "";
        lblError.Text = "";
        //btnSubmit.Text = "Submit";
        //btnClearForm.Text = "Clear";
    }

    protected void ibtBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("ViewProviderCarDriverDOB.aspx");
    }

    protected void btnDelete_Click(object sender, ImageClickEventArgs e)
    {
        rfvAuthCode.Validate();
        if (lblProviderCarDriverDOBId.Text != "0" && txtAuthCode.Text.Trim().Length > 0)
        {
            //Int64 _caryearid = CheckCarYear(Convert.ToInt32(ddlFromYear.Text), Convert.ToInt32(ddlToYear.Text), Convert.ToInt32(ddlOccupation.Text));
            Int64 _cardriverdobid = Convert.ToInt64(lblProviderCarDriverDOBId.Text);
            Int64 _providercompanyid = Convert.ToInt64(Session[General.ProviderCompanyId].ToString());
            string _authcode = CE.Security.MySecurity.Encrypt(txtAuthCode.Text, true);
            Int64 _refceuserid = Convert.ToInt64(Session[General.UserId].ToString());
            if (CheckAuthCode(_authcode, 100))
            {
                if (_cardriverdobid != 0)
                {
                    CarDriverDobDB _cardriverdobdb = new CarDriverDobDB();
                    if (_cardriverdobdb.Delete(_cardriverdobid, _providercompanyid))
                    {
                        InsertProviderAuthTransaction(_cardriverdobid, "CICarDriverAge", 3, 100, _authcode, _providercompanyid, _refceuserid);
                       Response.Redirect("ViewProviderCarDriverDOB.aspx");
                    }
                    else
                    {
                        lblMessage.Text = "Deleting failed.";
                    }
                }
            }
            else
            {
                lblMessage.Text = "Wrong auth code!";
            }
        }
    }

    public void EffectiveWhen(object sender, EventArgs e)
    {
        if (rdoEffectivity.SelectedValue == "Yes")
        {
            divNewRate.Visible = true;
        }
        else
        {
            divNewRate.Visible = false;
        }

        usrEffectivityDate.Day = CE.Global.General.GetUTCDateTime().Day;
        usrEffectivityDate.Month = CE.Global.General.GetUTCDateTime().Month;
        usrEffectivityDate.Year = CE.Global.General.GetUTCDateTime().Year;
    }


    protected bool CheckAuthCode(string strAuthCode, int intAuthCodeType)
    {
        bool _retValue = false;
        ProviderAuthCode _authcode = new ProviderAuthCode();
        ProviderAuthCodeDB _authcodedb = new ProviderAuthCodeDB();
        _authcode = _authcodedb.GetProviderAuthCode(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), intAuthCodeType, strAuthCode);
        if (_authcode != null)
        {
            _retValue = true;
        }

        return _retValue;
    }

    protected Int64 InsertProviderAuthTransaction(Int64 TransactionId, string TransactionCode, int TransactionType,
        int AuthCodeType, string AuthCode, Int64 ProviderCompanyId, Int64 RefCEUserId)
    {
        ProviderAuthTransaction _authtrans = new ProviderAuthTransaction();
        ProviderAuthTransactionDB _authtransdb = new ProviderAuthTransactionDB();

        _authtrans.TransactionId = TransactionId;
        _authtrans.TransactionCode = TransactionCode;
        _authtrans.TransactionType = TransactionType;
        _authtrans.AuthCode = AuthCode;
        _authtrans.AuthCodeType = AuthCodeType;
        _authtrans.ProviderCompanyId = ProviderCompanyId;
        _authtrans.RefCEUserID = RefCEUserId;

        _authtrans.AuthTransId = _authtransdb.Insert(_authtrans);
        return _authtrans.AuthTransId;
    }

    private void CheckMainPageAccess()
    {
        Int64 PageAccess = General.GetProviderMainPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), PageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
    }

    private void CheckSubPageAccess()
    {
        Int64 PageAccess = General.GetProviderSubPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), SubPageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
        else if (PageAccess == 100)
        {
            btnSubmit.Enabled = false;
            btnClearForm.Enabled = false;
            btnDelete.Enabled = false;
        }
        else if (PageAccess == 200)
        {
            btnSubmit.Enabled = true;
            btnClearForm.Enabled = true;
            btnDelete.Enabled = false;
        }
        else if (PageAccess == 300)
        {
            btnSubmit.Enabled = true;
            btnClearForm.Enabled = true;
            btnDelete.Enabled = true;
        }
    }

}
